package com.itheima.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*public class LoginUI {
    public static void main(String[] args) {
            JFrame frame = new JFrame("公司项目登录界面");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 300);
            frame.setLocationRelativeTo(null); // 居中显示

            // 设置背景颜色
            frame.getContentPane().setBackground(new Color(245, 245, 245)); // 灰白色背景

            // 创建面板
            JPanel panel = new JPanel();
            panel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(10, 10, 10, 10); // 内边距
            gbc.fill = GridBagConstraints.HORIZONTAL;

            // 标题标签
            JLabel titleLabel = new JLabel("欢迎登录");
            titleLabel.setFont(new Font("楷体", Font.BOLD, 24));
            titleLabel.setForeground(new Color(64, 64, 64)); // 深灰色文字
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 2;
            gbc.anchor = GridBagConstraints.CENTER;
            panel.add(titleLabel, gbc);

            // 用户名输入框
            gbc.gridy++;
            gbc.gridwidth = 1;
            gbc.anchor = GridBagConstraints.LINE_START;
            panel.add(new JLabel("用户名:"), gbc);
            gbc.gridx++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            JTextField usernameField = new JTextField(20);
            panel.add(usernameField, gbc);

            // 密码输入框
            gbc.gridy++;
            gbc.gridx = 0;
            gbc.fill = GridBagConstraints.NONE;
            panel.add(new JLabel("密码:"), gbc);
            gbc.gridx++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            JPasswordField passwordField = new JPasswordField(20);
            panel.add(passwordField, gbc);

            // 登录按钮
            gbc.gridy++;
            gbc.gridx = 0;
            gbc.gridwidth = 2;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            JButton loginButton = new JButton("登录");
            loginButton.setBackground(new Color(0, 128, 255)); // 蓝色按钮
            loginButton.setForeground(Color.WHITE);
            loginButton.setFont(new Font("楷体", Font.BOLD, 14));
            panel.add(loginButton, gbc);

            frame.add(panel);
            frame.setVisible(true);
    }
}
import javax.swing.*;
        import java.awt.*;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;*/

public class LoginUI extends JFrame {
        private JTextField usernameField;
        private JPasswordField passwordField;
        private JButton loginButton;
        private JButton cancelButton;

        public LoginUI() {
                initializeUI();
                setupComponents();
                setupEvents();
        }

        private void initializeUI() {
                setTitle("用户登录");
                setSize(400, 300);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                setLocationRelativeTo(null);
                setResizable(false);
                getContentPane().setBackground(new Color(240, 248, 255));
        }

        private void setupComponents() {
                JPanel panel = new JPanel();
                panel.setLayout(new GridBagLayout());
                panel.setBackground(new Color(240, 248, 255));
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(8, 8, 8, 8);

                // 标题标签
                JLabel titleLabel = new JLabel("用户登录");
                titleLabel.setFont(new Font("楷体", Font.BOLD, 24));
                titleLabel.setForeground(new Color(41, 128, 185));
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.gridwidth = 2;
                panel.add(titleLabel, gbc);

                // 用户名标签
                JLabel userLabel = createLabel("用户名:");
                gbc.gridy = 1;
                gbc.gridwidth = 1;
                panel.add(userLabel, gbc);

                // 用户名输入框
                usernameField = new JTextField(15);
                usernameField.setFont(new Font("楷体", Font.PLAIN, 14));
                usernameField.setBorder(BorderFactory.createCompoundBorder(
                        BorderFactory.createLineBorder(new Color(173, 216, 230), 2),
                        BorderFactory.createEmptyBorder(5, 8, 5, 8)));
                gbc.gridx = 1;
                gbc.gridy = 1;
                panel.add(usernameField, gbc);

                // 密码标签
                JLabel passLabel = createLabel("密  码:");
                gbc.gridx = 0;
                gbc.gridy = 2;
                panel.add(passLabel, gbc);

                // 密码输入框
                passwordField = new JPasswordField(15);
                passwordField.setEchoChar('*');
                passwordField.setFont(new Font("楷体", Font.PLAIN, 14));
                passwordField.setBorder(BorderFactory.createCompoundBorder(
                        BorderFactory.createLineBorder(new Color(173, 216, 230), 2),
                        BorderFactory.createEmptyBorder(5, 8, 5, 8)));
                gbc.gridx = 1;
                gbc.gridy = 2;
                panel.add(passwordField, gbc);

                // 登录按钮
                loginButton = createButton("登 录", new Color(46, 204, 113));
                gbc.gridx = 0;
                gbc.gridy = 3;
                gbc.anchor = GridBagConstraints.EAST;
                panel.add(loginButton, gbc);

                // 取消按钮
                cancelButton = createButton("取 消", new Color(231, 76, 60));
                gbc.gridx = 1;
                gbc.gridy = 3;
                gbc.anchor = GridBagConstraints.WEST;
                panel.add(cancelButton, gbc);

                add(panel, BorderLayout.CENTER);
        }

        private JLabel createLabel(String text) {
                JLabel label = new JLabel(text);
                label.setFont(new Font("楷体", Font.BOLD, 14));
                label.setForeground(new Color(52, 73, 94));
                return label;
        }

        private JButton createButton(String text, Color bgColor) {
                JButton button = new JButton(text);
                button.setFont(new Font("楷体", Font.BOLD, 14));
                button.setForeground(Color.WHITE);
                button.setBackground(bgColor);
                button.setFocusPainted(false);
                button.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
                button.setCursor(new Cursor(Cursor.HAND_CURSOR));
                button.addMouseListener(new java.awt.event.MouseAdapter() {
                        public void mouseEntered(java.awt.event.MouseEvent evt) {
                                button.setBackground(bgColor.darker());
                        }
                        public void mouseExited(java.awt.event.MouseEvent evt) {
                                button.setBackground(bgColor);
                        }
                });
                return button;
        }

        private void setupEvents() {
                loginButton.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                String username = usernameField.getText();
                                String password = new String(passwordField.getPassword());

                                if ("admin".equals(username) && "123456".equals(password)) {
                                        JOptionPane.showMessageDialog(LoginUI.this,
                                                "登录成功！", "提示", JOptionPane.INFORMATION_MESSAGE);
                                } else {
                                        JOptionPane.showMessageDialog(LoginUI.this,
                                                "用户名或密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
                                }
                        }
                });

                cancelButton.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                                System.exit(0);
                        }
                });
        }

        public static void main(String[] args) {
                SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                                new LoginUI().setVisible(true);
                        }
                });
        }
}